package com.rr.learningdemo.rabbitmq.controller;

import com.alibaba.fastjson.JSON;
import com.rr.learningdemo.rabbitmq.dto.MessageWithLevel;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhuangcanrun
 * @Date 2021/06/21/17:03
 * @description
 **/

@Controller
public class RabbitMQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ResponseBody
    @GetMapping("/sendToMq/{message}")
    public String send(@PathVariable("message")String message){
        rabbitTemplate.convertAndSend("exchangeForDistinct", "queueForDistinct", message);
        return message;
    }

    @ResponseBody
    @PostMapping("/sendMessageWithLevelToMq")
    public String send(@RequestBody List<MessageWithLevel> messages){
        rabbitTemplate.convertAndSend("exchangeForDistinct", "queueForDistinctWithLevel", JSON.toJSONString(messages));
        return messages.toString();
    }

    @ResponseBody
    @PostMapping("/sendListMessageToMq")
    public String sendListMessageToMq(@RequestBody List<MessageWithLevel> messages){
        rabbitTemplate.convertAndSend("exchangeForDistinct", "queueForListMessage", JSON.toJSONString(messages));
        return messages.toString();
    }
}
